#include "ui_multiline.h"
#include "ui_manager.h"

#include "lang/strman.h"
#include "lang/lang.h"
#include "font/font.h"

#include "util/timer.h"		    // Needs to come in early
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging

#include "script/scripthelper.h"
#include "script/write_lua.h"

#ifdef _ENABLE_LUABIND
#include <luabind/luabind.hpp>
#endif // _ENABLE_LUABIND


using namespace ui;
using namespace script;


UIMultiLine ::UIMultiLine (UIWidget* pParent,const std::string& Name) :
	UIWidget(pParent, Name)
{
	m_Type = WT_MULTILINE;
	Clear();
}


UIMultiLine ::~UIMultiLine ()
{

}

bool		UIMultiLine ::Create(const std::string& Name, const math::Vec2& Pos, const math::Vec2& Size)
{
	UIWidget::Create(Name, Pos, Size);
	// create progress stuff

	return true;
}
bool		UIMultiLine ::Load(LuaPlus::LuaObject& _LObj)
{
	UIWidget::Load(_LObj);	// get all the common stuff loaded


	return true;
}

void		UIMultiLine::SetTextA(const std::string& Text)
{
	SetTextW(lang::Lang::ConvertAtoW(Text));
}

void		UIMultiLine::SetTextW(const std::wstring& Text)
{
	m_BaseString = Text;
	std::wstring	LookupText = lang::StrMan::Instance().GetStringW(Text);
	if (LookupText.empty())
		LookupText = Text;
	Clear();	

	Font* pFont = (Font*)(m_Text.GetFont());
	if (!pFont) return;
	Graphics*			pGraph = GetGraphics();
	int					ScreenW = pGraph->GetScreenWidth();
	int					ScreenH = pGraph->GetScreenHeight();
	math::Vec2			Size = m_Size - (m_Text.m_Border*2);
	pFont->BuildGlyphData(LookupText, int(Size.x * ScreenW), int(Size.y* ScreenH), m_LineA, m_GlyphA, m_LineC, m_GlyphC, true);
}

bool		UIMultiLine::ExportSpecificsToFile(script::WriteLua*	pWL)
{
	// FIXME: output options
	return true;
}


bool		UIMultiLine ::Tick(float dt)
{
	UIWidget::Tick(dt);	// tick children, etc
	return true;
}

void	UIMultiLine ::ScrollUp()
{
	if (m_Offset > 0) m_Offset--;
}

void	UIMultiLine ::ScrollDown()
{
	int	MaxLines = (int)(GetSize().y / m_TextSize.y);


	if ((int)m_Offset <= (int)(1 + m_LineC-MaxLines) ) m_Offset++;
}

void	UIMultiLine ::Clear()
{
	m_LineC  = m_GlyphC = 0;
}


bool		UIMultiLine ::Render(bool Force)
{
	if (!UIWidget::Render(Force)) return false;
/*
	math::Vec2 Border = math::Vec2(0.01f, 0.005f);
	math::Vec2 Pos = GetAbsPosition() + Border;
	if (m_Offset > m_Lines.size() ) m_Offset = 0;
	for (unsigned int i=m_Offset; i<m_Lines.size(); i++)
	{
		UIText::TEXT_JUST_HORIZ	Orig = m_Lines[i].GetHorizJust();
		if (UIText::s_ForceLeft)
		{
			m_Lines[i].SetHorizJust(UIText::TJH_LEFT);
		}
		m_Lines[i].SetExtents(Pos, m_TextSize);
		Pos.y += Border.y + m_TextSize.y;
		if (Pos.y >= GetAbsPosition().y + GetSize().y)
			return true;
		m_Lines[i].Render();
		m_Lines[i].SetHorizJust(Orig);
	}
*/
	Font* pFont = (m_Text.GetFont());
	if (!pFont) return false;
//	if (m_LineC <= 0) return true;
	unsigned int Flags = 1;
	if (UIText::s_ForceLeft) Flags = 0;
	pFont->RenderGlyphData(m_Pos, m_Size-(m_Text.m_Border*2), m_Color, m_LineA, m_GlyphA, m_LineC, m_GlyphC, Flags, true);

	return true;
}

UIWidget*	UIMultiLine ::GetClone()
{
	UIMultiLine *	pNew = new UIMultiLine (m_pParent, GetCloneName(m_Name));
	FillCloneInfo((UIWidget*)pNew);

	pNew->m_Offset	=	this->m_Offset;
	pNew->SetTextW(this->m_Text.m_OriginalText);

	return pNew;
}

void	UIMultiLine ::RefreshText()
{
	SetTextW(m_BaseString);
	for (ElementList::iterator It = m_Set.begin(); It != m_Set.end(); It++)
		It->second->RefreshText();
}


/// register with LUA Bind
void	UIMultiLine ::Register(LuaPlus::LuaState* _pScript)
{
#ifdef _ENABLE_LUABIND
    lua_State * L = _pScript->GetCState ( );

	luabind::module(L)
    [
		luabind::class_<UIMultiLine , UIWidget >( "MultiLineWidget" )
		.def( "ScrollUp",		&UIMultiLine::ScrollUp)
		.def( "ScrollDown",		&UIMultiLine::ScrollDown)

	];
#endif //_ENABLE_LUABIND
}
